import routesConfig, { RoutesConfig } from '../config/routes-config';

// 支持类的方法和属性集合
export const version = 1;

export interface MenuTreeNode {
  title?: string;
  path?: string;
  origin?: any;
  // eslint-disable-next-line no-use-before-define
  next?: MenuTree;
  chainKey?: string;
}
export interface MenuTree {
  [keyname: string]: MenuTreeNode;
}
/**
 * @description: 根据路由配置生成菜单配置
 * @param {?RoutesConfig} 路由配置
 * @return {MenuTree} 菜单配置
 */
export function generateMenuConfigFromRoutesConfig(
  config: RoutesConfig = routesConfig,
) {
  const menuTree: MenuTree = {};
  const configs = config.filter((item) => !item.type);
  configs.forEach((routeItem) => {
    let head: MenuTree = menuTree;

    const lastIndex = routeItem.menuname.length - 1;
    let chainKey = '';
    routeItem.menuname.forEach((menunameItem, index) => {
      chainKey = `${chainKey}${index ? '-' : ''}${menunameItem}`;
      if (!head[menunameItem]) {
        head[menunameItem] = {
          title: menunameItem,
          chainKey,
        };
      }
      if (!head[menunameItem].next && index !== lastIndex) {
        head[menunameItem].next = {};
      }
      if (head[menunameItem].next) head = head[menunameItem].next as MenuTree;
    });
    const subNode = head[routeItem.menuname[lastIndex]];
    if (routeItem.asdefault || !subNode.origin) {
      subNode.path = routeItem.path;
      subNode.origin = routeItem;
    }
  });
  return menuTree;
}

export interface MenuGenerate {
  children: boolean;
  label?: string;
  code?: string;
  componentPath?: string;
  icon?: string;
  id?: string;
  menuType?: string;
  name?: string;
  parentId?: string;
  path?: string;
  sort?: string;
}

export function getParentIdList(tree: any, nodeId: any) {
  const { children = 'children', id = 'id' } = {};
  // eslint-disable-next-line no-shadow
  const toFlatArray = (tree: any, parentId?: any) =>
    tree.reduce((t: any, _: { [x: string]: any }) => {
      const child = _[children];
      return [
        ...t,
        parentId ? { ..._, parentId } : _,
        ...(child && child.length ? toFlatArray(child, _[id]) : []),
      ];
    }, []);
  const getIds = (flatArray: any[]) => {
    let ids = [nodeId];
    let child = flatArray.find((_) => _[id] === nodeId);
    while (child && child.parentId) {
      ids = [child.parentId, ...ids];
      // eslint-disable-next-line no-loop-func
      child = flatArray.find((_) => _[id] === child.parentId);
    }
    return ids;
  };
  return getIds(toFlatArray(tree));
}

export function getParentIdListss(list: any, parentId: any) {
  const data = list.find((item: any) => item.id === parentId);
  if (data) {
    return { id: data?.id, parentId: data.parentId };
  }
  return '';
}
export function getParentIdLists(list: any, Id: any, parentId: any) {
  const arr = [{ id: Id, parentId }];

  arr.forEach((item) => {
    const data = getParentIdListss(list, item?.parentId);
    if (data) {
      arr.unshift(data);
      const datas: any = getParentIdListss(list, data?.parentId);
      if (datas) {
        arr.unshift(datas);
        const datass: any = getParentIdListss(list, datas?.parentId);
        if (datass) {
          arr.unshift(datass);
        }
      }
    }
  });

  return arr.map((item) => item?.id);
}

export function generateTree(
  config: MenuGenerate[],
  list: any,
  // eslint-disable-next-line camelcase
  routerList: any,
) {
  // eslint-disable-next-line camelcase
  const newrouterList = routerList;

  list?.forEach((items: any) => {
    let nameList: string | any[] = [];
    // if (items?.parentId !== 0) {
    nameList = getParentIdLists(list, items.id, items?.parentId);
    // }
    const menuname = list
      ?.filter((item: any) => nameList.includes(item.id))
      .map((item: any) => item.name);
    let permissionList: any = [];
    if (items.menuType === 1) {
      permissionList = list
        .filter((i: any) => i.parentId === items.id)
        .map((it: any) => it.code);
    }

    newrouterList.push({
      menuname,
      asdefault: true,
      path: items?.path,
      component: items.componentPath,
      permission: permissionList,
    });
  });
  return routerList;
}
/**
 * @description: 根据路由配置生成菜单配置
 * @param {?RoutesConfig} 路由配置
 * @return {MenuTree} 菜单配置
 */
export function generateMenu(config: MenuGenerate[], list: any, key?: any) {
  const configs = generateTree(config, list, []);
  const menuTree = generateMenuConfigFromRoutesConfig(configs);
  const data: any = {};
  if (menuTree[key]) {
    const { title } = menuTree[key];
    if (title) {
      data[title] = menuTree[key];
    }
  }
  if (menuTree[key]) {
    return data;
  }
  return menuTree;
}

export function generateMenus(config: MenuGenerate[], list: any) {
  // eslint-disable-next-line eqeqeq
  const data: any = list?.filter(
    (item: { menuType: any }) => item.menuType !== 2,
  );
  const configs = generateTree(config, data, []);
  const menuTree = generateMenuConfigFromRoutesConfig(configs);
  return menuTree;
}
